package com.itheima.web.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.domain.QuestionItem;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.BeanUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {
    private QuestionItemService questionItemService = BeanFactory.getBean("questionItemService", QuestionItemService.class);
    //根据查询所有信息的方法
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //获取问题id
            String questionId = request.getParameter("questionId");
            //1 调用service，根据id查询信息
            List<QuestionItem> list=questionItemService.findAll(questionId);
            //2 响应结果到客户端
            return new Result(true, MessageConstant.QUERY_QUESTIONITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }
    //删除信息的方法
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数并封装
            String id = request.getParameter("id");
            //2 调用service层方法，批量删除数据
            questionItemService.delete(id);
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_QUESTIONITEM_FAIL);
        }
    }

    //修改数据的方法
    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数并封装
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            //2 调用service层方法，修改数据
            questionItemService.update(questionItem);
            //3 响应结果
            return new Result(true, MessageConstant.EDIT_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_QUESTIONITEM_FAIL);
        }
    }

    //根据id查询信息的方法
    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数 id
            String id = request.getParameter("id");
            //2 调用service，根据id查询信息
            QuestionItem questionItem=questionItemService.findById(id);
            //3 响应结果到客户端
            return new Result(true, MessageConstant.QUERY_QUESTIONITEM_SUCCESS,questionItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }

    //保存信息
    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数并封装
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            System.out.println(questionItem.toString());
            //2 调用service层方法，保存数据
            questionItemService.save(questionItem);
            //3 响应结果
            return new Result(true, MessageConstant.SAVE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SAVE_QUESTIONITEM_FAIL);
        }
    }
}
